package com.select;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.bean.sales.sales1Bean;

import com.bean.usersBean;
import com.google.gson.Gson;
import com.util.MyUtil;
import com.util.salesUtil;
import com.bean.lineItem;

@WebServlet("/salesServlet")
public class salesServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset:utf-8");
        resp.setCharacterEncoding("utf-8");
        /**
         * 查询订单表
         */
        if("1".equals(type)){
            try {
                String userId = req.getSession().getAttribute("uid").toString();
                usersBean users = MyUtil.selectUser(userId);
                String status = users.getType();
               List<lineItem> list = salesUtil.selectOrder(userId,status);
               resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 根据订单详情需要 查询订单表和商品表
         */
        if("2".equals(type)){
            String orderId = req.getParameter("orderId");
            try {
             lineItem lineitem = salesUtil.selectParticulars(orderId);
             resp.getWriter().write(new Gson().toJson(lineitem));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 添加退货订单
         */
        if("3".equals(type)){
            String userId = req.getSession().getAttribute("uid").toString();//业务员Id
            String orderId = req.getParameter("orderId");//订单编号
            String trackingNumber = req.getParameter("trackingNumber");//物流单号
            String trackingName = req.getParameter("trackingName");//物流公司
            String status = "0";//退货状态
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String create_time = df.format(date);
            try {
                salesUtil.insert(userId,orderId,trackingNumber,trackingName,status,create_time);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                salesUtil.update1(orderId);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 查询全部退货订单
         */
        if("4".equals(type)){
            try {
                List<sales1Bean> list = salesUtil.selectAll();
                resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 查询分页页数
         */
        if("9".equals(type)){
            try {
                String trackingNumber = req.getParameter("SelectNumber");//物流单号
                String name = req.getParameter("selectName");//产品名称
               sales1Bean sales =  salesUtil.selectLine(trackingNumber,name);
               Long line = sales.getLine();
                Double page= Math.ceil(line*0.1);
               sales.setPage(page.intValue());
               resp.getWriter().write(new Gson().toJson(sales));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 获取当前页数查询数据
         */
        if("10".equals(type)){
            String page = req.getParameter("page");//当前页数
            Integer data = (Integer.valueOf(page)-1)*10;
            String trackingNumber = req.getParameter("SelectNumber");//物流单号
            String name = req.getParameter("selectName");//产品名称
            try {
                List<sales1Bean> list = salesUtil.selectPage(trackingNumber,name,data);
                resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 修改退货状态
         */
        if("11".equals(type)){
            String id = req.getParameter("id");
            String amount = req.getParameter("amount");
            String orderId = req.getParameter("orderId");
            try {
                salesUtil.update(id);
                sales1Bean sales = salesUtil.select2(orderId);
                String number = sales.getNumber();
                salesUtil.update2(amount,number);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 根据条件查询退货订单
         */
        if("5".equals(type)){
            String trackingNumber = req.getParameter("SelectNumber");//物流单号
            String name = req.getParameter("selectName");//产品名称
            try {
                List<sales1Bean> list = salesUtil.select(trackingNumber,name);
                resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 修改退货信息
         */
        if("6".equals(type)){
            String trackingNumber = req.getParameter("trackingNumber");//物流单号
            String name = req.getParameter("trackingName");//商品名
            String id = req.getParameter("id");//退货id
            try {
                salesUtil.update(trackingNumber,name,id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 查询退货信息
         */
        if("7".equals(type)){
            String id = req.getParameter("id");
            try {
                sales1Bean sales1bean = salesUtil.select1(id);
                resp.getWriter().write(new Gson().toJson(sales1bean));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 删除退货订单
         */
        if("8".equals(type)){
            String id = req.getParameter("id");
            try {
                salesUtil.delete(id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
